//设置状态
import { ADULT, ADULTPWD, LAUNCHPWD, DEBUG, HISTORY, NOTRACE, VIDEOFULLSCREEN, VIDEOAUTOPLAY, AUDIOAUTOPLAY, VIDEOSLIDERENDED, THEMEMODE, DOCUMENTFONTSIZE, DOCUMENTPAGETYPE, DOCUMENTLINEHEIGHT, DOCUMENTBGCOLOR, DOCUMENTTEXTCOLOR, DOWNLOADPATH } from '../config.js'

const state = {
	documentFontSize: uni.getStorageSync(DOCUMENTFONTSIZE) || 20, //文档字体大小
	documentPageType: uni.getStorageSync(DOCUMENTPAGETYPE) || 'real', //文档翻页类型
	documentLineHeight: uni.getStorageSync(DOCUMENTLINEHEIGHT) || 15, //文档行间距
	documentBgColor: uni.getStorageSync(DOCUMENTBGCOLOR) || '', //文档背景色
	documentTextColor: uni.getStorageSync(DOCUMENTTEXTCOLOR) || '', //文档字体色
	themeMode: uni.getStorageSync(THEMEMODE) || 0, //主题模式
	adult: uni.getStorageSync(ADULT) || 0, //青壮年模式
	adultPwd: uni.getStorageSync(ADULTPWD) || '', //青壮年模式密码
	launchPwd: uni.getStorageSync(LAUNCHPWD) || '', //青壮年模式密码
	debug: uni.getStorageSync(DEBUG) || 0, //调试模式
	history: uni.getStorageSync(HISTORY) || 0, //历史记录
	notrace: uni.getStorageSync(NOTRACE) || 0, //浏览器无痕模式
	videoFullscreen: uni.getStorageSync(VIDEOFULLSCREEN) || 0, //浏览器视频全屏时方向
	videoAutoplay: uni.getStorageSync(VIDEOAUTOPLAY) || 0, //视频自动播放
	audioAutoplay: uni.getStorageSync(AUDIOAUTOPLAY) || 0, //音频自动播放
	videoSliderEnded: uni.getStorageSync(VIDEOSLIDERENDED) || 0, //滑动短视频播放结束
	downloadPath: uni.getStorageSync(DOWNLOADPATH) || '_doc/uniapp_save' //下载文件保存路径
}

const getters = {
	GET_DOCUMENT_FONT_SIZE () {
		return state.documentFontSize
	},
	GET_DOCUMENT_PAGE_TYPE (state) {
		return state.documentPageType
	},
	GET_DOCUMENT_LINE_HEIGHT (state) {
		return state.documentLineHeight
	},
	GET_DOCUMENT_BG_COLOR (state) {
		return state.documentBgColor
	},
	GET_DOCUMENT_TEXT_COLOR (state) {
		return state.documentTextColor
	},
	GET_SKIN_MODE (state) {
		if ( state.themeMode == 0 ) {
			return uni.getSystemInfoSync().osTheme == 'light' ? 'default' : 'night'
		} else {
			return state.themeMode == 1 ? 'default' : 'night'
		}
	},
	GET_THEME_MODE (state) {
		return state.themeMode
	},
	GET_DEBUG (state) {
		return state.debug
	},
	GET_HISTORY (state) {
		return state.history
	},
	GET_NOTRACE (state) {
		return state.notrace
	},
	GET_VIDEO_FULLSCREEN (state) {
		return state.videoFullscreen
	},
	GET_VIDEO_AUTOPLAY (state) {
		return state.videoAutoplay
	},
	GET_AUDIO_AUTOPLAY (state) {
		return state.audioAutoplay
	},
	GET_VIDEO_SLIDER_ENDED (state) {
		return state.videoSliderEnded
	},
	GET_DOWNLOAD_PATH (state) {
		return state.downloadPath
	},
	GET_ADULT (state) {
		return state.adult
	},
	GET_ADULT_PWD (state) {
		return state.adultPwd
	},
	GET_LAUNCH_PWD (state) {
		return state.launchPwd
	}
}

const mutations = {
	//设置文档字体大小
	SET_DOCUMENT_FONT_SIZE (state, value) {
		state.documentFontSize = value
		uni.setStorageSync(DOCUMENTFONTSIZE, value)
	},
	//设置文档翻页类型
	SET_DOCUMENT_PAGE_TYPE (state, value) {
		state.documentPageType = value
		uni.setStorageSync(DOCUMENTPAGETYPE, value)
	},
	//设置文档行间距
	SET_DOCUMENT_LINE_HEIGHT (state, value) {
		state.documentLineHeight = value
		uni.setStorageSync(DOCUMENTLINEHEIGHT, value)
	},
	//设置文档背景色
	SET_DOCUMENT_BG_COLOR (state, value) {
		state.documentBgColor = value
		uni.setStorageSync(DOCUMENTBGCOLOR, value)
	},
	//设置文档字体色
	SET_DOCUMENT_TEXT_COLOR (state, value) {
		state.documentTextColor = value
		uni.setStorageSync(DOCUMENTTEXTCOLOR, value)
	},
	//设置主题模式
	SET_THEME_MODE (state, value) {
		state.themeMode = value
		uni.setStorageSync(THEMEMODE, value)
	},
	//设置调试模式
	SET_DEBUG (state, value) {
		state.debug = value
		uni.setStorageSync(DEBUG, value)
	},
	//设置浏览历史
	SET_HISTORY (state, value) {
		state.history = value
		uni.setStorageSync(HISTORY, value)
	},
	//设置无痕模式
	SET_NOTRACE (state, value) {
		state.notrace = value
		uni.setStorageSync(NOTRACE, value)
	},
	//设置视频全屏方向
	SET_VIDEO_FULLSCREEN (state, value) {
		state.videoFullscreen = value
		uni.setStorageSync(VIDEOFULLSCREEN, value)
	},
	//设置视频自动播放
	SET_VIDEO_AUTOPLAY (state, value) {
		state.videoAutoplay = value
		uni.setStorageSync(VIDEOAUTOPLAY, value)
	},
	//设置音频自动播放
	SET_AUDIO_AUTOPLAY (state, value) {
		state.audioAutoplay = value
		uni.setStorageSync(AUDIOAUTOPLAY, value)
	},
	//设置短视频滑动播放结束操作
	SET_VIDEO_SLIDER_ENDED (state, value) {
		state.videoSliderEnded = value
		uni.setStorageSync(VIDEOSLIDERENDED, value)
	},
	//设置下载文件保存路径
	SET_DOWNLOAD_PATH (state, value) {
		state.downloadPath = value
		uni.setStorageSync(DOWNLOADPATH, value)
	},
	//设置青壮年模式
	SET_ADULT (state, value) {
		state.adult = value;
		uni.setStorageSync(ADULT, value)
	},
	//设置青壮年模式安全密码
	SET_ADULT_PWD (state, value) {
		state.adultPwd = value;
		uni.setStorageSync(ADULTPWD, value)
	},
	//设置启动安全密码
	SET_LAUNCH_PWD (state, value) {
		state.launchPwd = value;
		uni.setStorageSync(LAUNCHPWD, value)
	}
}

const actions = {
	//切换日间和夜间模式
	switchSkinMode ({getters, commit}) {
		const skinMode = getters['GET_SKIN_MODE'] == 'default' ? 2 : 1
		commit('SET_THEME_MODE', skinMode)
	}
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
	actions
}
